//
//  UtilsFunction.m
//  Massage
//
//  Created by hujlhappy on 13-7-29.
//  Copyright (c) 2013年 jiangtao. All rights reserved.
//

#import "UtilsFunction.h"
//mac address
#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#import "ZipArchive.h"
#import "CommConst.h"
#import "MBHUDView.h"

@implementation UtilsFunction

+(NSString *)getMacAddress
{
    int                    mib[6];
    size_t                len;
    char                *buf;
    unsigned char        *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl    *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1/n");
        return NULL;
    }
    
    if ((buf = (char *)malloc(len)) == NULL) {
        printf("Could not allocate memory. error!/n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    // NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    NSString *outstring = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    
    free(buf);
    
    return outstring;
    
}

+(void) saveURL:(NSString *)strURL
{
    //URL保存到本地
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:strURL forKey:URL_KEY];
    
    [defaults synchronize];
}

+(NSString *)getURL
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:URL_KEY];
}

+(void) setProductLastUpdateTime:(NSString *)lastUpdateTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:lastUpdateTime forKey:PRODUCT_LAST_UPDATE_TIME];
    
    [defaults synchronize];
}

+(NSString *)getProductLastUpdateTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *trnStr = [defaults objectForKey:PRODUCT_LAST_UPDATE_TIME];
    
    if(!trnStr){
        trnStr = @"";
    }
    
    return trnStr;
}

+(void) setRoomLastUpdateTime:(NSString *)lastUpdateTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:lastUpdateTime forKey:ROOM_LAST_UPDATE_TIME];
    
    [defaults synchronize];
}

+(NSString *)getRoomLastUpdateTime
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *trnStr = [defaults objectForKey:ROOM_LAST_UPDATE_TIME];
    
    if(!trnStr){
        trnStr = @"";
    }
    
    return trnStr;
}

+(void) setUserId:(NSString *)userId
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:userId forKey:CURRENT_USER_ID];
    
    [defaults synchronize];
}

+(NSString *)getUserId
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *trnStr = [defaults objectForKey:CURRENT_USER_ID];
    
    if(!trnStr){
        trnStr = @"unkown userId";
    }
    
    return trnStr;
}

+(void) setUserName:(NSString *)userName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:userName forKey:CURRENT_USER_NAME];
    
    [defaults synchronize];
}

+(NSString *)getUserName
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *trnStr = [defaults objectForKey:CURRENT_USER_NAME];
    
    if(!trnStr){
        trnStr = @"未知用户";
    }
    
    return trnStr;
}

+(NSString *)getDocumentDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return documentpath;
}

+(void)unZipFile:(NSString *)zipPath unZip:(NSString *)unZipPath
{
    ZipArchive* zip = [[ZipArchive alloc] init];
    
    if( [zip UnzipOpenFile:zipPath] )
    {
        BOOL ret = [zip UnzipFileTo:unZipPath overWrite:YES];
        if( NO==ret )
        {
        }
        [zip UnzipCloseFile];
    }
}

+(void)deleteFolder:(NSString *)folderPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    [fileManager removeItemAtPath:folderPath error:nil];
    
}

+(void)showHUDAdjustWidth:(NSString *)message
{
    MBHUDView *hud = [MBHUDView hudWithBody:message type:MBAlertViewHUDTypeDefault hidesAfter:HIDE_AFTER_TIME show:NO];
    
    float width = [message length]*18.0;
    if (width < HUD_MIN_WIDTH) {
        width = HUD_MIN_WIDTH;
    }else if (width > HUD_MAX_WIDTH){
        width = HUD_MAX_WIDTH;
    }
    
    hud.size = CGSizeMake(width, HUD_HEIGTH);
    [hud addToDisplayQueue];
}

@end
